package cn.tedu.charging.order.config;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MqttConfiguration {
    /**
     * localhost 连接地址 tcp 协议 1883 端口
     * */
    public String host = "tcp://localhost:1883";
    /**
     * 用户名
     * */
    private String username = "admin";
    /**
     * 密码
     * */
    private String password = "gqy1345350916";
    /**
     * 客户端id
     * */
    public String clientId = "order_service_client" + Math.random();

    @Bean
    public MqttClient mqttClient() throws MqttException {
        //通过链接地址和客户端创建MqttClient对象
        MqttClient mqttClient = new MqttClient(host, clientId);
        //创建MqttConnectOptions连接相关配置
        MqttConnectOptions options = new MqttConnectOptions();
        //配置用户名
        options.setUserName(username);
        //配置密码
        options.setPassword(password.toCharArray());
        //通过配置和emqx进行连接
        mqttClient.connect(options);
        return mqttClient;
    }
}
